﻿Imports System.Web.UI
Imports System.Collections.Generic

Imports DotNetNuke
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Services.Localization

Namespace dnnWerk.Modules.Nuntio.Content

    Partial Class uc_Preferences
        Inherits ContentBase


#Region "Delegates"

        Public Delegate Sub PreferencesUpdatedEventHandler(ByVal sender As Object, ByVal e As System.EventArgs)

#End Region

#Region "Events"

        Public Event PreferencesUpdated As PreferencesUpdatedEventHandler

#End Region

#Region "Private Members"

        Private _title As String = Null.NullString
        Private _content As String = Null.NullString
        Private _identifier As String = Null.NullString

        'Private _resourcefile As String = Me.ModuleDirectory & "controls/App_LocalResources/uc_MySettings.ascx"
        Public Overloads ReadOnly Property LocalResourceFile() As String
            Get
                Return Me.ModuleDirectory & "controls/App_LocalResources/uc_Preferences.ascx"
            End Get
        End Property

#End Region

#Region "Public Members"

        Public Property Title() As String
            Get
                Return _title
            End Get
            Set(ByVal value As String)
                _title = value
            End Set
        End Property

        Public Property Content() As String
            Get
                Return _content
            End Get
            Set(ByVal value As String)
                _content = value
            End Set
        End Property

        Public Property Identifier() As String
            Get
                Return _identifier
            End Get
            Set(ByVal value As String)
                _identifier = value
            End Set
        End Property

#End Region

#Region "Event Handlers"

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            If Not Page.IsPostBack Then
                Me.BindFallbackLocales(drpFallbackLocalePref)
                BindSettings()
            End If

            LocalizeForm()

        End Sub

        Private Sub cmdUpdatePreferences_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdUpdatePreferences.Click
            UpdateSettings()
            RaiseEvent PreferencesUpdated(Me, New System.EventArgs)
        End Sub

#End Region

#Region "Private Methods"

        Private Sub BindSettings()

            If ShowAllLocales Then
                rblShowLocales.SelectedIndex = 1
            Else
                rblShowLocales.SelectedIndex = 0
            End If

            If DefaultFallback <> "NONE" Then
                Try
                    drpFallbackLocalePref.SelectedValue = DefaultFallback
                Catch
                    'fallback locale doesn't exist anymore
                End Try
            End If

        End Sub

        Private Sub UpdateSettings()

            Try

                'show all locales?
                If rblShowLocales.SelectedIndex = 0 Then
                    Personalization.Personalization.SetProfile("NC_SETTINGS", "SHOW_ALL_LOCALES", "FALSE")
                Else
                    Personalization.Personalization.SetProfile("NC_SETTINGS", "SHOW_ALL_LOCALES", "TRUE")
                End If

                'default fallback mode
                If drpFallbackLocalePref.SelectedIndex > 0 Then
                    Personalization.Personalization.SetProfile("NC_SETTINGS", "DEFAULT_FALLBACK", drpFallbackLocalePref.SelectedValue)
                Else
                    Personalization.Personalization.SetProfile("NC_SETTINGS", "DEFAULT_FALLBACK", "NONE")
                End If

                Me.lblResult.Text = Localization.GetString("UpdateSettingsComplete", Me.LocalResourceFile)

            Catch ex As Exception

                Me.lblResult.Text = Localization.GetString("UpdateSettingsError", Me.LocalResourceFile)

            End Try

        End Sub

        Private Sub LocalizeForm()
            lblLocales.Text = Localization.GetString("lblLocales", Me.LocalResourceFile)
            Me.rblShowLocales.Items(0).Text = Localization.GetString("EnabledLocales", Me.LocalResourceFile)
            Me.rblShowLocales.Items(1).Text = Localization.GetString("AllLocales", Me.LocalResourceFile)
            Me.cmdUpdatePreferences.Text = Localization.GetString("cmdUpdatePreferences", Me.LocalResourceFile)
            Me.lblPrefHead.Text = Localization.GetString("lblPrefHead", Me.LocalResourceFile)
            Me.lblFallbackLocalePref.Text = Localization.GetString("lblFallbackLocalePref", Me.LocalResourceFile)
            Me.drpFallbackLocalePref.Items(0).Text = Localization.GetString("NoFallback", Me.LocalResourceFile)
        End Sub


#End Region




    End Class

End Namespace
